package com.central.apps.util;

import com.central.app.common.base.BaseHandler;
import com.central.app.common.cache.CfgRedisCache;
import com.central.app.common.constant.AppConstant;
import com.central.app.common.util.AppCommonUtil;
import com.central.app.model.BizApp;
import com.central.app.model.BizObject;
import com.central.app.model.CommonModel;
import com.central.common.auth.details.LoginAppUser;
import com.central.common.util.SysUserUtil;
import com.central.common.util.ToolUtil;
import org.springframework.stereotype.Service;

@Service
public class LoginUserUtil {

    public static String getDeptList(String app, String table,CfgRedisCache cfgRedisCache) throws Exception {
        BizApp appinfo = cfgRedisCache.getCacheApp(app);
        BizObject object = cfgRedisCache.getCacheObjByApp(app);
        BaseHandler handler = null;

        if (ToolUtil.isNotEmpty(table)) {
            object = cfgRedisCache.getCacheObject(table);
        }
        CommonModel commonModel = new CommonModel();

        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();
        boolean isPermission = true;//原来是true,现在改成false,子表不进行数据权限限制，只根据配置的关联关系去配置
        if (AppConstant.USER_ADMIN.equals(loginAppUser.getUsername())) {
            isPermission = false;
        }

        if (isPermission) {
            if (AppConstant.BASE_NO.equals(appinfo.getDatalevel()) || ToolUtil.isEmpty(appinfo.getDatalevel())) {
                AppCommonUtil.setDataAuthorization(commonModel, appinfo, object, handler);
            }
        }

        return commonModel.getDsf();
    }

}
